package com.wanhelibs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.base.DishTag;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhelibs.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XieZaiWei on 2017/8/9.
 * 口味选择Adapter
 */

public class TagSelectAdapter extends BaseAdapter {

	private LayoutInflater	mInflater;
	private Dish			mDish;
	private List <DishTag>	mArrTag;

	public TagSelectAdapter(Context context, Dish dish) {
		mInflater = LayoutInflater.from(context);
		mDish = dish;
		mArrTag = mDish.getDish_tagsList();;
	}

	@Override
	public int getCount() {
		if (mArrTag != null) {
			return mArrTag.size();
		} else {
			return 0;
		}

	}

	@Override
	public DishTag getItem(int position) {
		return mArrTag.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.tag_select_item, null);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.iv_reduce = (ImageView) convertView.findViewById(R.id.iv_reduce);
			viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
			viewHolder.iv_add = (ImageView) convertView.findViewById(R.id.iv_add);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final DishTag dishTag = mArrTag.get(position);
		viewHolder.tv_name.setText(dishTag.getName());
		viewHolder.tv_count.setText(String.valueOf(getCount(dishTag)));

		viewHolder.iv_add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int step = CartManager.getInstance().getAddStep(mDish.getDish_guid());
				if (step != 0) {
					List <DishTag> arrDishTag = new ArrayList<>(1);
					arrDishTag.add(dishTag);
					CartManager.getInstance().addItemByTag(mDish.getDish_guid(), step, arrDishTag);
				}
				notifyDataSetChanged();
			}
		});

		viewHolder.iv_reduce.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int step = CartManager.getInstance().getReduceStep(mDish.getDish_guid());
				if (step != 0) {
					List <DishTag> arrDishTag = new ArrayList<>(1);
					arrDishTag.add(dishTag);
					CartManager.getInstance().reduceItemByTag(mDish.getDish_guid(), step,
							arrDishTag);
				}
				notifyDataSetChanged();
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		public TextView		tv_name;
		public ImageView	iv_reduce;
		public TextView		tv_count;
		public ImageView	iv_add;

	}

	private int getCount(DishTag dishTag) {
		CartItem cartItem = new CartItem();
		cartItem.setDish(mDish);
		List <DishTag> dishTags = new ArrayList<>(1);
		dishTags.add(dishTag);
		cartItem.setTags(dishTags);
		int count = CartManager.getInstance().getItemCount(cartItem);
		return count;
	}
}
